package com.cpecc.controller.wuzi;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cpecc.mail.SendMailThread;
import com.cpecc.manager.system.EmailManager;
import com.cpecc.manager.system.HuanJieManager;
import com.cpecc.manager.system.RootManager;
import com.cpecc.manager.system.ShiYongZTManager;
import com.cpecc.manager.system.ToDoManager;
import com.cpecc.manager.system.UserManager;
import com.cpecc.manager.system.YouJianManager;
import com.cpecc.manager.wuzi.BaoFeiManager;
import com.cpecc.manager.wuzi.BuPeiManager;
import com.cpecc.manager.wuzi.FenTanManager;
import com.cpecc.manager.wuzi.HuanHuoManager;
import com.cpecc.manager.wuzi.TuiHuoManager;
import com.cpecc.manager.wuzi.WeiXiuManager;
import com.cpecc.manager.wuzi.WuZiManager;
import com.cpecc.manager.wuzi.YiJiaoJLManager;
import com.cpecc.manager.wuzi.YiJiaoManager;
import com.cpecc.model.system.Dep;
import com.cpecc.model.system.Email;
import com.cpecc.model.system.ToDo;
import com.cpecc.model.system.User;
import com.cpecc.model.system.YouJian;
import com.cpecc.model.wuzi.WuZi;
import com.cpecc.model.wuzi.YiJiao;
import com.cpecc.model.wuzi.YiJiaoJL;

@Controller
@RequestMapping(value = "/wuzi")
public class TuiHuiController {

	@Autowired
	private UserManager userManager;
	
	@Autowired
	private YiJiaoManager yiJiaoManager;
	
	@Autowired
	private HuanJieManager huanJieManager;
	
	@Autowired
	private WuZiManager wuZiManager;
	
	@Autowired
	private YiJiaoJLManager yiJiaoJLManager;
	
	@Autowired
	private ShiYongZTManager shiYongZTManager;
	
	@Autowired
	private FenTanManager fenTanManager;
	
	@Autowired
	private ToDoManager toDoManager;
	
	@Autowired
	private WeiXiuManager weiXiuManager;
	
	@Autowired
	private BaoFeiManager baoFeiManager;
	
	@Autowired
	private HuanHuoManager huanHuoManager;
	
	@Autowired
	private TuiHuoManager tuiHuoManager;
	
	@Autowired
	private BuPeiManager buPeiManager;
	
	@Autowired
	private YouJianManager youJianManager;
	
	@Autowired
	private EmailManager emailManager;
	
	@Autowired
	private RootManager rootManager;
	
	private List<User> getXiaYiBuR(String tuiHuiId){
		List<User> list=new ArrayList<User>();
		YiJiao tuiHui=yiJiaoManager.get(tuiHuiId);
		int huanJie=tuiHui.getHuanJie().getId();
		switch(huanJie){
		case 602: list.addAll(userManager.getGZGLY()); break;
		}
		return list;
	}
	
	@RequestMapping(value = "/tuiHuiSQ")
	public ModelAndView tuiHuiSQ(String id) {
		ModelAndView mav=new ModelAndView();
		mav.addObject("wuZiId", id);
		mav.setViewName("tuihui/tuiHui");
		return mav;
	}
	
	@RequestMapping(value = "/tuiHuiTJ")
	public void tuiHuiTJ(String wuZiId,int lx,String yiJiaoYY,HttpServletResponse response){
		if(weiXiuManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}			
		else if(yiJiaoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		else if(baoFeiManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(huanHuoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(tuiHuoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(buPeiManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WuZi wuZi=wuZiManager.get(wuZiId);
		YiJiao yiJiao=new YiJiao();
		yiJiao.setBuBan(0);
		yiJiao.setHuanJie(huanJieManager.get(602));
		yiJiao.setLiuChengLX(lx);
		yiJiao.setShenQingRQ(new Date());
		yiJiao.setWuZi(wuZi);
		yiJiao.setYiJiaoR(user);
		yiJiao.setYiJiaoYY(yiJiaoYY);
		yiJiaoManager.save(yiJiao);
		YiJiaoJL yiJiaoJL=new YiJiaoJL();
		yiJiaoJL.setCaoZuoRQ(new Date());
		yiJiaoJL.setHuanJie(huanJieManager.get(601));
		yiJiaoJL.setPiFuLX("退还申请提交");
		yiJiaoJL.setPiFuNR("提交");
		yiJiaoJL.setUser(user);
		yiJiaoJL.setYiJiao(yiJiao);
		yiJiaoJLManager.save(yiJiaoJL);
		
		toDoManager.deleteList(yiJiao);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(602));
			toDo.setTask("退还接收");
			toDo.setType("退还");
			toDo.setUser(user1);
			toDo.setYiJiao(yiJiao);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(602);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 602, user1, youJian);
				st.setYiJiao(yiJiao);
				st.setWuZi(wuZi);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/myTuiHui")
	public ModelAndView myTuiHui(){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		ModelAndView mav=new ModelAndView();
		mav.addObject("tuiHuiList", yiJiaoManager.getMyTuiHui(user));
		mav.setViewName("tuihui/myTuiHui");
		return mav;
	}
	
	@RequestMapping(value = "/tuiHuiDaiBan")
	public ModelAndView tuiHuiDaiBan(){
		Subject currentUser = SecurityUtils.getSubject();		
		ModelAndView mav=new ModelAndView();
		if(currentUser.hasRole("固资管理员")){
			mav.addObject("tuiHuiList", yiJiaoManager.getAllTuiHui());
		}
		mav.setViewName("tuihui/tuiHuiDaiBan");
		return mav;
	}
	
	@RequestMapping(value = "/getTuiHuiShu")
	public void getTuiHuiShu(HttpServletResponse response){
		int zongShu=0;
		Subject currentUser = SecurityUtils.getSubject();
		if(currentUser.hasRole("固资管理员"))
			zongShu=yiJiaoManager.getAllTuiHui().size();
		response.setContentType("application/json;charset=utf-8");		
		String result="{\"result\":\"("+zongShu+")\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	@RequestMapping(value = "/tuiHuanQueRen")
	public void tuiHuanQueRen(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		Dep dep=userManager.getUserDep(user);
		YiJiao yiJiao=yiJiaoManager.get(id);
		yiJiao.setHuanJie(huanJieManager.get(603));
		yiJiaoManager.cover(yiJiao);
		YiJiaoJL yiJiaoJL=new YiJiaoJL();
		yiJiaoJL.setCaoZuoRQ(new Date());
		yiJiaoJL.setHuanJie(huanJieManager.get(602));
		yiJiaoJL.setPiFuLX("固资管理员接收");
		yiJiaoJL.setPiFuNR("同意");
		yiJiaoJL.setYiJiao(yiJiao);
		yiJiaoJL.setUser(user);
		yiJiaoJLManager.save(yiJiaoJL);
		WuZi wuZi=yiJiao.getWuZi();
		wuZi.setShiYongZT(shiYongZTManager.get(105));
		wuZi.setGuanLiBM(dep);
		wuZi.setDep(dep);
		wuZi.setUser(user);
		wuZiManager.cover(wuZi);
		fenTanManager.quChuFenTan(wuZi, new Date());
		toDoManager.deleteList(yiJiao);
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/tuiHuiDetail")
	public ModelAndView tuiHuiDetail(String id){
		YiJiao yiJiao=yiJiaoManager.get(id);
		ModelAndView mav=new ModelAndView();
		mav.addObject("yiJiao", yiJiao);
		mav.addObject("xiaYiBuR", getXiaYiBuR(id));
		mav.addObject("yiJiaoJLList", yiJiaoJLManager.getAllJL(id));
		mav.setViewName("tuihui/tuiHuiDetail");
		return mav;
	}
	
	@RequestMapping(value = "/myTuiHuiSP")
	public ModelAndView myTuiHuiSP(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("yiJiaoSPList", yiJiaoJLManager.getMyTHSPJL(user));
		mav.setViewName("tuihui/myTuiHuiSP");
		return mav;
	}
	
	@RequestMapping(value = "/thqrListAjax")
	public ModelAndView thqrList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("tuiHuiList", yiJiaoManager.getAllTuiHui());
		mav.setViewName("tuihui/thqrListAjax");
		return mav;
	}	
	
	@RequestMapping(value = "/tuiHuiBoHui")
	public ModelAndView tuiHuiBoHui(String id){	
		ModelAndView mav=new ModelAndView();
		mav.addObject("tuiHuiId", id);
		mav.setViewName("tuihui/tuiHuiBoHui");
		return mav;
	}
	
	@RequestMapping(value = "/tuiHuiBoHuiSave")
	public void tuiHuiBoHuiSave(String tuiHuiId,String boHuiYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		YiJiao yiJiao=yiJiaoManager.get(tuiHuiId);
		int dangQianHuanJie=yiJiao.getHuanJie().getId();
		yiJiao.setHuanJie(huanJieManager.get(604));
		yiJiaoManager.cover(yiJiao);
		YiJiaoJL yiJiaoJL=new YiJiaoJL();
		yiJiaoJL.setCaoZuoRQ(new Date());
		yiJiaoJL.setHuanJie(huanJieManager.get(dangQianHuanJie));
		yiJiaoJL.setPiFuLX("退回驳回");
		yiJiaoJL.setPiFuNR(boHuiYY);
		yiJiaoJL.setUser(user);
		yiJiaoJL.setYiJiao(yiJiao);
		yiJiaoJLManager.save(yiJiaoJL);
		//退回驳回
		toDoManager.deleteList(yiJiao);
		
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}	
	
	@RequestMapping(value = "/thjsPL")
	public void thjsPL(HttpServletRequest request,HttpServletResponse response){
		String result="{\"result\":\"none\"}";
		String type[]=request.getParameterValues("thqrId");
		if(type!=null)
			result="{\"result\":\"success\"}";
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/thjsBHPL")
	public ModelAndView thjsBHPL(HttpServletRequest request){	
		ModelAndView mav=new ModelAndView();
		String type[]=request.getParameterValues("thqrId");
		if(type!=null){
			String thqrIds="";
			for(int i=0;i<type.length;i++){
				thqrIds=thqrIds+type[i]+",";
			}
			if(thqrIds.length()>0){
				thqrIds=thqrIds.substring(0, thqrIds.length()-1);
			}
			mav.addObject("thqrIds", thqrIds);
		}
		mav.setViewName("tuihui/thbhPL");
		return mav;
	}
	
	@RequestMapping(value = "/thbhPLSave")
	public void thbhPLSave(String thqrIds,String boHuiYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String thqrId[]=thqrIds.split(",");
		for(int i=0;i<thqrId.length;i++){
			YiJiao yiJiao=yiJiaoManager.get(thqrId[i]);
			int dangQianHuanJie=yiJiao.getHuanJie().getId();
			yiJiao.setHuanJie(huanJieManager.get(604));
			yiJiaoManager.cover(yiJiao);
			YiJiaoJL yiJiaoJL=new YiJiaoJL();
			yiJiaoJL.setCaoZuoRQ(new Date());
			yiJiaoJL.setHuanJie(huanJieManager.get(dangQianHuanJie));
			yiJiaoJL.setPiFuLX("退回驳回");
			yiJiaoJL.setPiFuNR(boHuiYY);
			yiJiaoJL.setUser(user);
			yiJiaoJL.setYiJiao(yiJiao);
			yiJiaoJLManager.save(yiJiaoJL);
			//退回驳回
			toDoManager.deleteList(yiJiao);
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/thqrPiSubmit")
	public void thqrPiSubmit(HttpServletRequest request,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		Dep dep=userManager.getUserDep(user);
		String type[]=request.getParameterValues("thqrId");
		for(int i=0;i<type.length;i++){
			YiJiao yiJiao=yiJiaoManager.get(type[i]);
			yiJiao.setHuanJie(huanJieManager.get(603));
			yiJiaoManager.cover(yiJiao);
			YiJiaoJL yiJiaoJL=new YiJiaoJL();
			yiJiaoJL.setCaoZuoRQ(new Date());
			yiJiaoJL.setHuanJie(huanJieManager.get(602));
			yiJiaoJL.setPiFuLX("固资管理员接收");
			yiJiaoJL.setPiFuNR("同意");
			yiJiaoJL.setYiJiao(yiJiao);
			yiJiaoJL.setUser(user);
			yiJiaoJLManager.save(yiJiaoJL);
			WuZi wuZi=yiJiao.getWuZi();
			wuZi.setShiYongZT(shiYongZTManager.get(105));
			wuZi.setGuanLiBM(dep);
			wuZi.setDep(dep);
			wuZi.setUser(user);
			wuZiManager.cover(wuZi);
			fenTanManager.quChuFenTan(wuZi, new Date());
			
			toDoManager.deleteList(yiJiao);
		}		
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
		}
	}
}
